using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class Article
    {
        #region Article Private Fields

        int _articleId;
        string _articleName = string.Empty;
        string _articleBriefDescription = string.Empty;
        string _articleDescription = string.Empty;
        bool _isActive;
        bool _isDeleted;
        DateTime _createDate;
        ReadWriteCustomCollection<Article> _relatedArticles;
        #endregion

        #region Article Constractor
        public Article() { }

        public Article
        (
            int articleId,
            string articleName,
            string articleBriefDescription,
            string articleDescription,
            bool isActive,
            bool isDeleted,
            DateTime createDate
        )
        {
            this._articleId = articleId;
            this._articleName = articleName;
            this._articleBriefDescription = articleBriefDescription;
            this._articleDescription = articleDescription;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
        }
        public Article
        (
            string articleName,
            string articleBriefDescription,
            string articleDescription,
            bool isActive,
            bool isDeleted
        )
        {
            this._articleName = articleName;
            this._articleBriefDescription = articleBriefDescription;
            this._articleDescription = articleDescription;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
        }
        #endregion

        public int ArticleId
        {
            get { return _articleId; }
        }

        public string ArticleName
        {
            get { return _articleName; }
            set { _articleName = value; }
        }

        public string ArticleBriefDescription
        {
            get { return _articleBriefDescription; }
            set { _articleBriefDescription = value; }
        }

        public string ArticleDescription
        {
            get { return _articleDescription; }
            set { _articleDescription = value; }
        }

        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }
        public DateTime CreateDate
        {
            get { return this._createDate; }
        }

        public ReadWriteCustomCollection<Article> RelatedArticles
        {
            get
            {
                _relatedArticles = new ReadWriteCustomCollection<Article>();
                ECommerce.DAL.CustomCollection<TblArticleRelated> related = TblArticleRelated.GetTblArticleRelatedByArticleId(this._articleId);
                if (related != null)
                {
                    for (int i = 0; i < related.Count; i++)
                    {
                        try
                        {
                            Article article = Article.Load(related[i].RelatedArticleId);
                            if (article != null && article.IsActive)
                            {
                                _relatedArticles.Add(article);
                            }
                        }
                        catch { }
                    }
                }
                return this._relatedArticles;
            }
        }

        public void Create()
        {

            try
            {
                this._articleId = TblArticle.CreateNewTblArticle(this._articleName, this._articleBriefDescription, this._articleDescription, this._isActive, this._isDeleted, DateTime.Now);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblArticle tblArticle = TblArticle.GetTblArticleByArticleId(this._articleId);
                tblArticle.ArticleName = this._articleName;
                tblArticle.ArticleBriefDescription = this._articleBriefDescription;
                tblArticle.ArticleDescription = this._articleDescription;
                tblArticle.IsActive = this._isActive;
                tblArticle.IsDeleted = this._isDeleted;
                return tblArticle.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            /**************************************************
             * will update database table, will delete physical
             * files and will write in admin log about the 
             * delete
             **************************************************/
            this._isDeleted = true;
            this.Update();
        }

        public void AddRelatedArticles(Article article)
        {
            if (article.ArticleId == this.ArticleId)
                return;
            ECommerce.DAL.CustomCollection<TblArticleRelated> relateds = TblArticleRelated.GetTblArticleRelatedByArticleId(this._articleId);
            if (relateds != null)
            {
                for (int i = 0; i < relateds.Count; i++)
                {
                    if (article.ArticleId == relateds[i].RelatedArticleId)
                    {
                        return;
                    }
                }
            }
            this.RelatedArticles.Add(article);
            TblArticleRelated related = new TblArticleRelated();
            TblArticleRelated.CreateNewTblArticleRelated(this._articleId, article._articleId);
        }

        public void DeleteRelatedArticles(int articleId)
        {
             ECommerce.DAL.CustomCollection<TblArticleRelated> related = TblArticleRelated.GetTblArticleRelatedByArticleId(this._articleId);
            if (related != null)
            {
                for (int i = 0; i < related.Count; i++)
                {
                    if (related[i].RelatedArticleId == articleId) {
                        TblArticleRelated.DeleteTblArticleRelated(related[i].TblArticleRelatedId);
                        break;
                    }
                }
            }
        }

        public static ReadWriteCustomCollection<Article> Articles
        {
            get
            {
                ReadWriteCustomCollection<Article> articles = LoadFromCustomCollection(TblArticle.GetAllTblArticle());
                return articles;
            }
        }

        public static void DeleteArticles(ReadWriteCustomCollection<Article> articles)
        {
            foreach (Article article in articles)
            {
                article.Delete();
            }
        }

        public static ReadWriteCustomCollection<Article> GetArticles(int howMany)
        {
            try
            {
                ECommerce.DAL.CustomCollection<TblArticle> tblArticle = TblArticleExtended.GetActiveArticleByCount(howMany);
                ReadWriteCustomCollection<Article> articles = LoadFromCustomCollection(tblArticle);
                return articles;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Article Load(int articleId)
        {
            try
            {
                TblArticle tmpTblArticle = TblArticle.GetTblArticleByArticleId(articleId);
                Article article = new Article(tmpTblArticle.ArticleId, tmpTblArticle.ArticleName, tmpTblArticle.ArticleBriefDescription, tmpTblArticle.ArticleDescription, tmpTblArticle.IsActive, tmpTblArticle.IsDeleted, tmpTblArticle.CreateDate);

                return article;

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static ReadWriteCustomCollection<Article> GetBackendArticleListByArticleName(string ArticleName, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblArticleExtended.GetAllArticlesByArticleNamePaged(ArticleName, orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblArticleExtended.GetNonDeletedArticlesByArticleNamePaged(ArticleName, orderBy, startIndex, pageSize));
            }
        }

        public static ReadWriteCustomCollection<Article> GetBackendArticleList(bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblArticle.GetAllTblArticlePaged(orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblArticleExtended.GetNonDeletedArticlePaged(orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendArticleListCount(bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblArticle.GetAllTblArticlePagedCount("", 0, 0);
            }
            else
            {
                return TblArticleExtended.GetNonDeletedArticleCount();
            }
        }

        public static int GetBackendArticleListByArticleNameCount(string ArticleName, bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblArticleExtended.GetAllArticlesByArticleNameCount(ArticleName);
            }
            else
            {
                return TblArticleExtended.GetNonDeletedArticlesByArticleNameCount(ArticleName);
            }
        }

        private static ReadWriteCustomCollection<Article> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblArticle> tblArticle)
        {
            ReadWriteCustomCollection<Article> result = new ReadWriteCustomCollection<Article>();
            for (int i = 0; i < tblArticle.Count; i++)
            {
                TblArticle tmpTblArticle = tblArticle[i];
                result.Add(new Article(tmpTblArticle.ArticleId, tmpTblArticle.ArticleName, tmpTblArticle.ArticleBriefDescription, tmpTblArticle.ArticleDescription, tmpTblArticle.IsActive, tmpTblArticle.IsDeleted, tmpTblArticle.CreateDate));
            }
            return result;
        }

    }
}
